package cuong.ptit.androidvn;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import cuong.ptit.andoridvn.R;
import cuong.ptit.androidvn.helper.JSONParser;
import cuong.ptit.androidvn.loadimage.ImageLoader;

/*
 * @author LeCuong 
 * @version 1.0
 */

public class GetUser extends Activity {
	private ProgressDialog pDialog;
	private ImageView imgAvata;
	private int loader = R.drawable.loader;
	private ImageLoader imageLoader;

	private TextView tv_username, tv_gender, tv_messages, tv_register, tv_mem,
			tv_warning, tv_like, tv_trophy;

	private String id, username, gender, messages, register, mem, warning,
			like, trophy, mod, admin, url_img;
	private String getUser;

	JSONParser jsonParser = new JSONParser();

	// url get Json
	private final static String url_user = "http://android.vn/api.php?action=getUser";
	private final static String url_avatar = "http://android.vn/api.php?action=getAvatar";
	// item json
	private final static String TAG_ID = "user_id";
	private final static String TAG_USERNAME = "username";
	private final static String TAG_GENDER = "gender";
	private final static String TAG_MESSAGE = "message_count";
	private final static String TAG_RESGITER = "register_date";
	private final static String TAG_TROPHY = "trophy_points";
	private final static String TAG_MOD = "is_moderator";
	private final static String TAG_ADMIN = "is_admin";
	private final static String TAG_LIKE = "like_count";
	private final static String TAG_WARNING = "warning_points";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_user_activity);
		LoadUI();
		imageLoader = new ImageLoader(getApplicationContext());

		// get info in login
		Intent i = getIntent();
		String user = i.getStringExtra("user_name");
		String hash = i.getStringExtra("user_hash");
		getUser = user + ":" + hash;
		Log.d("hash", getUser);

		new LoadGetUser().execute();

	}

	private void LoadUI() {
		tv_mem = (TextView) findViewById(R.id.tv_mem);
		tv_username = (TextView) findViewById(R.id.tv_username);
		tv_gender = (TextView) findViewById(R.id.tv_gender);
		tv_messages = (TextView) findViewById(R.id.tv_messagess_count);
		tv_register = (TextView) findViewById(R.id.tv_register_date);
		tv_mem = (TextView) findViewById(R.id.tv_mem);
		tv_warning = (TextView) findViewById(R.id.tv_warning_point);
		tv_like = (TextView) findViewById(R.id.tv_like_count);
		tv_trophy = (TextView) findViewById(R.id.tv_trophy);
		imgAvata = (ImageView) findViewById(R.id.img_avata);

	}

	// load get Uset
	class LoadGetUser extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			pDialog = new ProgressDialog(GetUser.this);
			pDialog.setMessage("Loading user....");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... args) {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("hash", getUser));

			String json_avatar = jsonParser.makeHttpRequest(url_avatar, "GET",
					params);
			String json_user = jsonParser.makeHttpRequest(url_user, "GET",
					params);

			Log.d("Link user", json_avatar);

			try {

				JSONObject a = new JSONObject(json_avatar);
				if (a != null) {
					url_img = a.getString("avatar");

					int star = 18;
					int end = url_img.length();
					url_img.substring(17, 50);

				}
				

				JSONObject c = new JSONObject(json_user);
				if (c != null) {

					id = c.getString(TAG_ID);
					username = c.getString(TAG_USERNAME);
					admin = c.getString(TAG_ADMIN);
					mod = c.getString(TAG_MOD);
					gender = c.getString(TAG_GENDER);
					like = c.getString(TAG_LIKE);
					messages = c.getString(TAG_MESSAGE);
					mod = c.getString(TAG_MOD);

					// date register

					register = c.getString(TAG_RESGITER);
					trophy = c.getString(TAG_TROPHY);
					warning = c.getString(TAG_WARNING);
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			pDialog.dismiss();

			runOnUiThread(new Runnable() {
				public void run() {
					// set text

					tv_username.setText(username);

					if (gender.equals("male")) {
						tv_gender.setText("Nam");
					} else {
						tv_gender.setText("Nu");
					}

					if (admin.equals("0") && mod.equals("0")) {
						tv_mem.setText("Member");
					} else if (admin.equals("1") && mod.equals("0")) {
						tv_mem.setText("Admin");
					} else if (admin.equals("0") && mod.equals("1")) {
						tv_mem.setText("Mod");
					}

					tv_like.setText(like);
					tv_trophy.setText(trophy);
					tv_messages.setText(messages);

					// get date

					SimpleDateFormat ford = new SimpleDateFormat("dd/MM/yyyy");
					String date = ford.format(new Date(1367172451));

					tv_register.setText(date);

					if (warning == null) {
						tv_warning.setText("0");
					} else {
						tv_warning.setText(warning);
					}

					// set img avata
					int a = url_img.length();
					
					imageLoader.DisplayImage(url_img, loader,
							imgAvata);

				}
			});
			super.onPostExecute(result);
		}
	}

}
